<?php
require_once(DP_BASE_DIR."/modules/masterdata/mdcostcenter.class.php");
require_once(DP_BASE_DIR."/modules/masterdata/mddivision.class.php");
$cc_id = intval( dPgetParam( $_GET, 'cc_id', 0 ) );

// check permissions for this record
/*
$perms =& $AppUI->acl();
if (! ($canEdit = $perms->checkModuleItem( 'currencys', 'edit', $currency_id )) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
*/

// load the record data
$msg = '';
$row = new CMdCostCenter();

$canDelete = $row->canDelete( $msg, $currency_id );
if($msg == $AppUI->_('CostCenterDeleteUserError', UI_OUTPUT_JS)) {
	$userDeleteProtect=true;
}

if ($view) {
	$view = ' readonly="true"';
}
else {
	unset($view);
}

if (!$row->load( $cc_id ) && $cc_id > 0) {
	$AppUI->setMsg( 'Cost Center' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}
$division_list = null;

$q = new DBQuery();
$q->addTable('costcenters');
$q->addQuery('cc_code');
if (isset($row->cc_code)) {
	$q->addWhere("cc_id != $row->cc_id");
}
$quoteCodeList = $q->loadColumn();
$qCL = '["' . implode('", "', $quoteCodeList) . '"]';


if (!$view) {
	$division_list  = getDivisionList();
	$division_list = arrayMerge( array( '0'=>'' ), $division_list );
?>
<form name="MdCostCenterForm" action="?m=masterdata" method="post">
<input type="hidden" name="dosql" value="mdcostcenter_do_aed" />
<input type="hidden" name="cc_id" value="<?=$cc_id?>" />
<?}?>
<table border="0" cellpadding="8" cellspacing="0" width="400px">
<tr>
<td colspan="2" bgcolor="#E0E0E0"><b><?=$AppUI->_('Cost Center Information')?></td>
</tr>
<tr>
	<td colspan="2">
		<table border="0" cellpadding="0" cellspacing="4" width="100%">
		<tr>
			<td align="right"> * <?=$AppUI->_('Code')?></td>
			<td>
				<input type="text" class="text" name="cc_code" id="cc_code"
				value="<?=@$row->cc_code?>" maxlength="20" style="width: 98%" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"> * <?=$AppUI->_('Name')?></td>
			<td>
				<input type="text" class="text" name="cc_name" id="cc_name"
					value="<?=@$row->cc_name?>" maxlength="150" style="width: 98%" <?=$view?>/>
			</td>
		</tr>
		<? if (!$view) { ?>
		<tr>
			<td align="right"><?=$AppUI->_('Division')?></td>
			<td>
				<?= arraySelect($division_list,'cc_division',' style="width: 98%"',@$row->cc_division)?>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Active')?></td>
			<td><input type="checkbox" name="cc_active"
					value="<?=@$row->cc_active?>"  <?=$view?> <?= (@$row->cc_active == 1) ? "checked":""?>/>
			</td>
		</tr>
		<? }else { ?>
		<tr>
			<td align="right"><?=$AppUI->_('Division')?></td>
			<td><? echo @$row->getDivisionName();?> </td>
		</tr>
		<? } ?>
		</table>
	</td>
</tr>
<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0">
	<? if (!$view) { ?>
		<input type="submit" class="button" value="<?=$AppUI->_('Save')?>"/>
		<input type="button" class="button" value="<?=$AppUI->_('Cancel')?>" onClick="doCloseCostCenter()"/>
	<? } else { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Close')?>" onClick="doCloseCostCenter()"/>
	<? } ?>
	</td>
</tr>
</table>
</form>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fCCCode = new LiveValidation('cc_code');
	fCCCode.add(Validate.Presence );
	fCCCode.add( Validate.Exclusion , {within: <?=$qCL?>});
	var fCCName = new LiveValidation('cc_name');
	fCCName.add(Validate.Presence );


	new Tips($$('.text'));
</div>
